
package ch.hearc.meteo.imp.afficheur.real;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import gnu.io.SerialPort;

import ch.hearc.meteo.spec.meteo.MeteoPortDetectionServiceFactory;
import ch.hearc.meteo.spec.meteo.MeteoPortDetectionService_I;

public class JPanelComOptions extends JPanel
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/

	public JPanelComOptions(JDialogComOptions dialogComOptions)
		{
		this.dialogComOptions = dialogComOptions;
		this.detectionService = MeteoPortDetectionServiceFactory.create();
		this.ok = false;
		geometry();
		control();
		apparence();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	/**
	 *  ok = true si l'utilisateur clique sur valider
	 *  ok = false si l'utilisateur clique sur annuler
	 */
	public String getStationName()
		{
		return textfieldStationName.getText();
		}

	public double getStationLatitude()
		{
		try
			{
			return Double.valueOf(textfieldStationLatitude.getText());
			}
		catch (Exception e)
			{
			return 0;
			}
		}

	public double getStationLongitude()
		{
		try
			{
			return Double.valueOf(textfieldStationLongitude.getText());
			}
		catch (Exception e)
			{
			return 0;
			}
		}

	public boolean getOk()
		{
		return this.ok;
		}

	public String getPortName()
		{
		return (String)this.comboBoxPortName.getSelectedItem();
		}

	public int getSpeed()
		{
		return (int)this.comboBoxPortSpeed.getSelectedItem();
		}

	public int getDataBits()
		{
		return (int)this.comboBoxPortDataBits.getSelectedItem();
		}

	public int getParity()
		{
		switch((String)this.comboBoxPortParity.getSelectedItem())
			{
			case "EVEN":
				return SerialPort.PARITY_EVEN;
			case "ODD":
				return SerialPort.PARITY_ODD;
			case "MARK":
				return SerialPort.PARITY_MARK;
			case "SPACE":
				return SerialPort.PARITY_SPACE;
			default:
				return SerialPort.PARITY_NONE;
			}
		}

	public int getStopBits()
		{
		switch((String)this.comboBoxPortStopBits.getSelectedItem())
			{
			case "2":
				return SerialPort.STOPBITS_2;
			case "1.5":
				return SerialPort.STOPBITS_1_5;
			default:
				return SerialPort.STOPBITS_1;
			}
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	private void geometry()
		{
		GridLayout layout = new GridLayout(11, 2);
		this.setLayout(layout);

		labelPortName = new JLabel("Port : ");
		labelPortSpeed = new JLabel("Vitesse : ");
		labelPortDataBits = new JLabel("Bits de donnée : ");
		labelPortParity = new JLabel("Parité : ");
		labelPortStopBits = new JLabel("Bits d'arrêt : ");
		labelStationName = new JLabel("Nom station : ");
		labelStationLatitude = new JLabel("Latitude : ");
		labelStationLongitude = new JLabel("Longitude : ");

		comboBoxPortName = createComboBoxPortName();
		comboBoxPortSpeed = createComboBoxPortSpeed();
		comboBoxPortDataBits = createComboBoxPortDataBits();
		comboBoxPortParity = createComboBoxPortParity();
		comboBoxPortStopBits = createComboBoxPortStopBits();

		textfieldStationName = new JTextField();
		textfieldStationLatitude = new JTextField();
		textfieldStationLongitude = new JTextField();

		boutonOk = new JButton("Valider");
		boutonAnnuler = new JButton("Annuler");
		boutonOk.setEnabled(false);

		add(labelStationName);
		add(textfieldStationName);
		add(labelStationLatitude);
		add(textfieldStationLatitude);
		add(labelStationLongitude);
		add(textfieldStationLongitude);

		//Ligne vide
		add(new JLabel());
		add(new JLabel());

		add(labelPortName);
		add(comboBoxPortName);
		add(labelPortSpeed);
		add(comboBoxPortSpeed);
		add(labelPortDataBits);
		add(comboBoxPortDataBits);
		add(labelPortParity);
		add(comboBoxPortParity);
		add(labelPortStopBits);
		add(comboBoxPortStopBits);

		//Ligne vide
		add(new JLabel());
		add(new JLabel());

		add(boutonOk);
		add(boutonAnnuler);
		}

	private void control()
		{
		ActionListener validateActionListener = new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent event)
					{
					boutonOk.doClick();
					}
			};

		textfieldStationName.addActionListener(validateActionListener);
		textfieldStationLatitude.addActionListener(validateActionListener);
		textfieldStationLongitude.addActionListener(validateActionListener);

		boutonOk.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent event)
					{
					ok = true;
					dialogComOptions.dispose();
					}
			});
		boutonAnnuler.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent event)
					{
					ok = false;
					dialogComOptions.dispose();
					}
			});

		textfieldStationName.setInputVerifier(new InputVerifier()
			{

				@Override
				public boolean verify(JComponent component)
					{
					return !textfieldStationName.getText().isEmpty();
					}
			});

		textfieldStationLatitude.setInputVerifier(new InputVerifier()
			{

				@Override
				public boolean verify(JComponent input)
					{
					try
						{
						String text = textfieldStationLatitude.getText();
						double latitude = Double.parseDouble(text);
						return (latitude >= -90 && latitude <= 90);
						}
					catch (Exception e)
						{
						return false;
						}
					}
			});

		textfieldStationLongitude.setInputVerifier(new InputVerifier()
			{

				@Override
				public boolean verify(JComponent input)
					{
					try
						{
						String text = textfieldStationLongitude.getText();
						double longitude = Double.parseDouble(text);
						return (longitude >= -180 && longitude <= 180);
						}
					catch (Exception e)
						{
						return false;
						}
					}
			});

		KeyListener keyListener = new KeyAdapter()
			{

				@Override
				public void keyReleased(KeyEvent e)
					{
					textfieldStationName.validate();
					textfieldStationLatitude.validate();
					textfieldStationLongitude.validate();

					if (textfieldStationName.getInputVerifier().verify(textfieldStationName) && textfieldStationLatitude.getInputVerifier().verify(textfieldStationLatitude) && textfieldStationLongitude.getInputVerifier().verify(textfieldStationLongitude) && comboBoxPortName.getItemCount() > 0)
						{
						boutonOk.setEnabled(true);
						}
					else
						{
						boutonOk.setEnabled(false);
						}
					}
			};

		textfieldStationName.addKeyListener(keyListener);
		textfieldStationLatitude.addKeyListener(keyListener);
		textfieldStationLongitude.addKeyListener(keyListener);
		}

	private void apparence()
		{
		this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		}

	private JComboBox<String> createComboBoxPortName()
		{
		List<String> listPortName = detectionService.findPortNameMeteo();

		//TODO : Supprimer
		String[] tabPortName = listPortName.toArray(new String[listPortName.size()]);

		return new JComboBox<String>(tabPortName);
		}

	private JComboBox<String> createComboBoxPortStopBits()
		{
		String[] tabPortStopBits = { "1", "1.5", "2" };
		JComboBox<String> comboBox = new JComboBox<String>(tabPortStopBits);
		comboBox.setSelectedItem("1");
		return comboBox;
		}

	private JComboBox<String> createComboBoxPortParity()
		{
		String[] tabPortParity = { "EVEN", "ODD", "MARK", "NONE", "SPACE" };
		JComboBox<String> comboBox = new JComboBox<String>(tabPortParity);
		comboBox.setSelectedItem("NONE");
		return comboBox;
		}

	private JComboBox<Integer> createComboBoxPortSpeed()
		{
		Integer[] tabPortSpeed = { 4800, 9600, 19200, 38400, 57600, 115200 };
		JComboBox<Integer> comboBox = new JComboBox<Integer>(tabPortSpeed);
		comboBox.setSelectedItem(57600);
		return comboBox;
		}

	private JComboBox<Integer> createComboBoxPortDataBits()
		{
		Integer[] tabPortDataBits = { SerialPort.DATABITS_5, SerialPort.DATABITS_6, SerialPort.DATABITS_7, SerialPort.DATABITS_8 };
		JComboBox<Integer> comboBox = new JComboBox<Integer>(tabPortDataBits);
		comboBox.setSelectedItem(SerialPort.DATABITS_8);
		return comboBox;
		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	//Tools
	private MeteoPortDetectionService_I detectionService;
	private JDialogComOptions dialogComOptions;
	private boolean ok;

	private JLabel labelPortName;
	private JLabel labelPortSpeed;
	private JLabel labelPortDataBits;
	private JLabel labelPortStopBits;
	private JLabel labelPortParity;
	private JLabel labelStationName;
	private JLabel labelStationLatitude;
	private JLabel labelStationLongitude;

	private JComboBox<String> comboBoxPortName;
	private JComboBox<String> comboBoxPortStopBits;
	private JComboBox<String> comboBoxPortParity;
	private JComboBox<Integer> comboBoxPortSpeed;
	private JComboBox<Integer> comboBoxPortDataBits;

	private JTextField textfieldStationName;
	private JTextField textfieldStationLatitude;
	private JTextField textfieldStationLongitude;

	private JButton boutonOk;
	private JButton boutonAnnuler;

	}
